<template>
  <div>

    <div class="mt-[32px]">
      <el-form label-width="85px" :rules="rules" :model="form" ref="FormRef">
        <el-form-item :label="item.name" :prop="item.code"
                      v-for="(item,index) in config"
                      :key="index"
        >

          <el-input v-model="form[item.code]"
                    v-if="!item.type"
          />
          <el-input v-model="form[item.code]"
                    v-if="item.type == 'number'"
                    type="number"
          />

          <el-input v-model="form[item.code]"
                    type="textarea"
                    :rows="5"
                    v-if="item.type == 'textarea'"
          />

          <el-select v-model="form[item.code]"
                     v-if="item.type == 'select'"
          >
            <el-option v-for="(it,idx) in item.list" :key="idx"
                       :label="it[item.label]" :value="it[item.value]"
            />
          </el-select>

          <UploadImg v-model="form[item.code]" v-if="item.type == 'upload'"/>

        </el-form-item>

      </el-form>
    </div>

  </div>
</template>
<script setup lang="ts">
import UploadImg from "@/components/UploadFile/src/UploadImg.vue"
import Icon from "@/components/Icon/src/Icon.vue";
import {listToMap, formError, deepCopy} from "@/utils";


import {OneThaliApi} from "@/api/system/tenant/flow";

import {DICT_TYPE, getIntDictOptions, getStrDictOptions} from "@/utils/dict";

const flowType = getStrDictOptions(DICT_TYPE.FLOW_TYPE);
const payType = getStrDictOptions(DICT_TYPE.PAY_TYPE);

const props = defineProps({
  info: {
    type: Object,
    required: false,
    default: () => {
    }
  }
})

const FormRef = ref(null);

const provinceData = ref([])

const config = ref([
  {
    name: "席位单价",
    code: "amount",
    type:"number",
    required: true,
  },
])

const rules = computed(() => {
  let map = listToMap(config.value, 'code');

  for (let i in map) {
    if (map[i].required) {
      if (map[i].type == 'select' || map[i].type == 'upload') {
        map[i] = [{required: true, message: `请选择${map[i].name}`, trigger: 'change'}]
      } else {
        map[i] = [{required: true, message: `请输入${map[i].name}`, trigger: 'blur'}]
      }
    }

  }
  return map
})


const form = ref({
  limitedDuration: 1
})

const confirm = async (callback) => {

  console.log("1111111111111")
  try {
    let res = await FormRef.value.validate();

    console.log(res)

    let option = {
      thali:"席位单价",
      type:payType.find(at=>at.label == '席位')?.value,
      dataValue:1,
      flowType:2,
      ...form.value,
      amount:form.value.amount * 100
    }

    console.log("????")


    if (form.value.id) {
      await OneThaliApi.updateOneThali(option);
    } else {
      await OneThaliApi.createOneThali(option);
    }
    callback && callback()
  } catch (e) {
    console.log(e, "eee")
    formError()
  }

}



onMounted(() => {

})

defineExpose({
  confirm
})

watch(() => props.info, () => {
  form.value = props.info;
}, {
  deep: true,
  immediate: true
})
</script>
<style lang="scss" scoped>

</style>
